<template>
  <table :id="id">
    <thead>
      <tr>
        <th v-for="(head, index) in heads" :key="index">{{ head }}</th>
      </tr>
    </thead>
    <tbody v-if="isNormal">
      <tr v-for="(r, ri) in rows1" :key="ri">
        <td v-for="(name, di) in r" :key="di">{{ name }}</td>
      </tr>
    </tbody>
    <tbody v-else>
      <tr>
        <td :colspan="`${heads.length}`">{{ Html }}</td>
      </tr>
      <tr v-for="(r, ri) in rows1" :key="'row1' + ri">
        <td v-for="(name, di) in r" :key="'row1' + di">{{ name }}</td>
      </tr>
      <tr>
        <td :colspan="`${heads.length}`">{{ Html2 }}</td>
      </tr>
      <tr v-for="(r, ri) in rows2" :key="'row2' + ri">
        <td v-for="(name, di) in r" :key="'row2' + di">{{ name }}</td>
      </tr>
    </tbody>
  </table>
</template>

<script>
import { export_table_to_excel } from "@utils/export2table";

export default {
  props: {
    isNormal: Boolean,
    heads: {
      type: Array,
      default: () => [],
    },
    rows1: {
      type: Array,
      default: () => [],
    },
    rows2: {
      type: Array,
      default: () => [],
    },
    Html: {
      type: String,
      default: () => "",
    },
    Html2: {
      type: String,
      default: () => "",
    },
  },
  data() {
    return {
      id: "",
    };
  },
  created() {
    this.id = "export_" + new Date();
  },
  methods: {
    setThStyle(th, flag) {
      let col = flag ? "66bf3b" : "409eff";
      th.s = {
        fill: {
          fgColor: { rgb: col },
        },
        alignment: {
          wrapText: false,
        },
      };
    },
    export(fileName) {
      export_table_to_excel(this.id, fileName, (ws) => {
        const reg = /[a-zA-Z]+[2]$/;
        const reg2 = RegExp(`[a-zA-Z]+[${this.rows1.length * 1 + 3}]$`);
        for (let key in ws) {
          if (ws[key].v) {
            if (reg.test(key)) {
              this.setThStyle(ws[key], true);
            }
            if (reg2.test(key)) {
              this.setThStyle(ws[key], false);
            }
          }
        }
      });
    },
    exportMonth(fileName) {
      export_table_to_excel(this.id, fileName);
    },
  },
};
</script>

<style>
</style>